<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<script>
  function exist (board, word) {
		const row = board.length
    const col = board[0].length

	  function find (r, c, cur) {
			if (r >= row || r < 0) return false
      if (c >= col || c < 0) return false
      if (board[r][c] !== word[cur]) return false

      if (cur === word.length - 1) return true

      const letter = board[r][c]
      board[r][c] = null

      const rst =
        find(r - 1, c, cur + 1) ||
	      find(r + 1, c, cur + 1) ||
	      find(r, c - 1, cur + 1) ||
        find(r, c + 1, cur + 1)

		  board[r][c] = letter

      return rst
    }

    for (let i = 0; i < row; i ++) {
			for (let j = 0; j < col; j ++) {
				const rst = find(i, j, 0)
        if (rst) {
					return rst
        }
      }
    }

		return false
  }

	const board = [
		['A', 'B', 'C', 'E'],
    ['S', 'F', 'C', 'S'],
    ['A', 'D', 'E', 'E']
  ]
  const word = 'ABCCED'
  console.log(exist(board, word))
</script>
</body>
</html>
